package com.tnmi.handler;

import com.tnmi.base.R;
import com.tnmi.exception.BizException;
import com.tnmi.exception.code.ExceptionCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @version 1.0
 * @Classname GlobalExceptionHandler
 * @Description TODO
 * @Date 2023/7/9 11:08
 * @Created by tnmi
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.error();
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error().code(ExceptionCode.ARITHMETICEXCEPTION.getCode()).msg(ExceptionCode.ARITHMETICEXCEPTION.getMsg());
    }

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public R error(BizException e){
        e.printStackTrace();
        return R.error().code(e.getCode()).msg(e.getMessage());
    }
}
